package com.pluginsacceptor.plugin.formula.MathsTrigo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observer;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.pluginsacceptor.plugin.formula.MathsTrigo.support.CellPositionParser;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataChooser;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataListener;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataSender;


public class SommeListener implements ActionListener {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(SommeListener.class);

    /**
     * Observable
      */
    private final DataListener dataListener;

    /** */
    private final DataSender dataSender;

    /**
     * Constructor
     * @param dataListener
     */
    public SommeListener(final DataListener dataListener, final DataSender dataSender) {
        super();
        this.dataListener = dataListener;
        this.dataSender = dataSender;
    }

    @Override
	public void actionPerformed(final ActionEvent e) {

        final String resultat = JOptionPane.showInputDialog(null, "Set resultat cell please",
        		"title", JOptionPane.QUESTION_MESSAGE);
        //todo add a parser A2 = xy

        final CellPositionParser parser = new CellPositionParser(resultat);

        if (parser.validate()) {
            final DataChooser dataChooser = new DataChooser();
            final Observer observer = dataChooser.getObserver();
            this.dataListener.addObserver(observer);

            dataChooser.setButtonListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        final double result = calculateSomme(dataChooser);
                        dataSender.sendDate(
                            parser.getRow(),
                            parser.getColumn(),
                            String.valueOf(result)
                        );
                        dataChooser.dispose();
                        dataListener.deleteObserver(observer);
                    }
                }
            );
        } else {
            warningPopup(resultat);
        }
	}

    private static void warningPopup(final String resultat) {
        LOG.warn("Invalid Cellule resultat " + resultat);
        JOptionPane.showMessageDialog(
            null,
            "Invalid Cellule resultat " + resultat + " (example 'A 1')",
            "warning",
            JOptionPane.ERROR_MESSAGE
        );
    }

    private static double calculateSomme(final DataChooser dataChooser) {
        double result = 0;
        for (final String tmp : dataChooser.getMathData()) {


            try {
                result += Double.parseDouble(tmp);
            } catch (final Exception e) {
                LOG.warn("invalid data value '" + tmp + "'");
            }
        }
        return result;
    }
}